import time

from qfluentwidgets import FluentIcon

from ok.color.Color import black_color
from ok.feature.FindFeature import FindFeature
from ok.logging.Logger import get_logger
from ok.task.TriggerTask import TriggerTask
from src.task.BaseXtTask import BaseXtTask

logger = get_logger(__name__)


class AutoDialogYs(TriggerTask, BaseXtTask, FindFeature):

    def __init__(self):
        super().__init__()
        self.name = "自动剧情"
        self.description = "自动过废话"
        self.icon = FluentIcon.SHOPPING_CART
        self.last_check_black_screen_time = None  # 上次检查黑屏的时间
        # self.info_set('is_world_task', True)  # 设置为世界任务，不会忽略

    def run(self):
        # is_first = True
        # while self.in_dialog():
        #     if not is_first:
        #         time.sleep(0.1)
        #         self.next_frame()
        #     is_first = False
        return False

            # if not self.find_one('ys_f', box=self.f_search_box(),
            #                      threshold=0.8):
            #     box_collect_img = self.get_box_by_name("box_collect_img")
            #     if boxs := self.find_feature('ys_f_ignore', box=box_collect_img):
            #         self.click_box(box=boxs[0])
            #         # return True
            # self.click_box(box=self.get_box_by_name("box_dialog_close"))

            # # 判断是否中间有黄色的字体
            # if self.is_box_yellow_font():
            #     self.box_next()
            # else:
            #
            #     percent = self.calculate_color_percentage(box=box_next, color=yellow_font_color)
            #     logger.debug(f'box_next color percent {percent} {box_next}')
            #     if percent > 0.02:
            #         self.click_box(box=self.get_box_by_name("box_next"))

            # return True
        # self.send_key('f')
        # self.sleep(0.2)
        # self.send_key('f')
        # self.sleep(0.2)
        # self.send_key('f')
        # self.sleep(0.2)
        # pg.press('f')
        # self.sleep(0.2)
        # pg.press('f')
        # self.sleep(0.2)
        # pg.press('f')
        # self.sleep(0.2)

    def f_search_box(self):
        f_search_box = self.get_box_by_name('box_f')
        # f_search_box = f_search_box.copy(x_offset=-f_search_box.width / 2,
        #                                  width_offset=f_search_box.width,
        #                                  height_offset=f_search_box.height * 9,
        #                                  y_offset=-f_search_box.height * 5,
        #                                  name='search_dialog')
        return f_search_box

    def trigger(self):
        # if not self.check_login():
        #     return False

        if self.in_dialog():
            return True
        # return False

    def in_dialog(self):
        # self.check_full_black()
        if self.click_jump():
            self.wait_until(lambda: self.click_confirm(), time_out=1, wait_until_before_delay=0.05)
            return True
        if box := self.find_one("ys_close_d1", box=self.get_box_by_name("box_close_d1")):
            self.click_box(box)
            self.sleep(0.1)
            return True
        # if box := self.find_one("ys_close_d2", box=self.get_box_by_name("box_close_d2")):
        #     self.click_box(box)
        #     self.sleep(0.1)
        #     return True
        if self.click_confirm(): return True
        if self.click_next(): return True

        if self.press_f():
            return True

        return False

    def click_jump(self):
        if box := self.find_one("xt_jump", box=self.get_box_by_name("box_jump")):
            self.click_box(box)
            return True
        if box := self.find_one("xt_jump2", box=self.get_box_by_name("box_jump")):
            self.click_box(box)
            return True
    def click_confirm(self):
        if box := self.find_one("xt_confirm", box=self.get_box_by_name("box_confirm")):
            self.click_box(box)
            return True
    def click_next(self):
        if box := self.find_one("xt_next", box=self.get_box_by_name("box_next")):
            # box=box.copy(x_offset=box.width*3)
            # self.click_box(box)
            self.send_key('space')
            return True
        if box := self.find_one("xt_next1", box=self.get_box_by_name("box_next"), threshold=0.6):
            # box=box.copy(x_offset=box.width*5)
            # self.click_box(box)
            self.send_key('space')
            return True
    def press_f(self):
        if box := self.find_one("xt_f1", box=self.get_box_by_name("box_f")):
            self.send_key('1')
            return True
        if box := self.find_one("xt_f2", box=self.get_box_by_name("box_f")):
            self.send_key('2')
            return True

    def check_full_black(self):
        if not self.last_check_black_screen_time or time.time() - self.last_check_black_screen_time > 2:
            self.last_check_black_screen_time = time.time()
            box_next = self.get_box_by_name("box_next")
            percent = self.calculate_color_percentage(box=box_next, color=black_color)
            logger.debug(f'box_next 黑屏判断 percent {percent} {box_next}')
            if percent > 0.7:
                self.click_box(box=box_next)
                time.sleep(0.5)

            if box := self.find_one('ys_note_page'):
                self.click_box(box=self.get_box_by_name("ys_note_page_close"))
                time.sleep(0.5)

            # if box:=self.find_one('ys_close', box=self.get_box_by_name("box_close")):
            #     self.click_box(box=box)
            #     time.sleep(0.5)


